#!/usr/bin/env groovy
package com.yidao.www

def autoBuild(project, branch, manuallyTrigger){

    def vueBuildProfile = vueBuildProfile(project, branch)
    def date = new Date()

    def label = vueBuildProfile.label + '_' + BUILD_NUMBER
    def image = vueBuildProfile.image
    def gitUrl = vueBuildProfile.gitUrl
    def buildDir = vueBuildProfile.buildDir
    def buildScript = vueBuildProfile.buildScript
    def ansible = vueBuildProfile.ansible
    def tag = vueBuildProfile.tag
    def artifactName = vueBuildProfile.artifactName
    def artifactTar = vueBuildProfile.artifactTar
    def artifactory = vueBuildProfile.artifactory
    def webhookUrl = vueBuildProfile.webhookUrl
    def base = new base()
    def ansibleBuild = new ansibleBuild()
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def username = ''
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def versionInfo = project + '_' + branch + '_' + buildNumber
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL = srverUrl + '/' + deployTarget

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins') {

            node(label){

                stage('checkout'){
                    try{
                        if(manuallyTrigger){
                            base.submoduleCheckout(gitUrl, branch)
                        }else{
                            base.submoduleGerritCheckout(gitUrl, branch)
                        }
                    }catch(e){
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', 'FAILURE')
                        println '代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('compile'){
                    try{
                        if(buildDir){
                            dir(buildDir){
                                base.npmBuild(buildScript)
                            }
                        }else{
                            base.npmBuild(buildScript)
                        }
                    }catch(e){
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', 'FAILURE')
                        println '打包失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('package'){
                    if(buildDir){
                        dir(buildDir){
                            base.zipPackage(artifactTar, artifactName)
                        }
                    }else{
                        base.zipPackage(artifactTar, artifactName)
                    }
                }

                stage('deploy to artifactory'){
                    print 'deploy ...'
                    print '================='
                    print artifactName
                    print deployTarget
                    print '================='
                    if(ansible){
                        ansibleBuild.autoDeploy(tag)
                    }
                    try{
                        if(buildDir){
                            dir(buildDir){
                                base.fileUpload(artifactTar, deployTarget)
                            }
                        }else{
                            base.fileUpload(artifactTar, deployTarget)
                        }
                    }catch(e){
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', 'FAILURE')
                        println '上传失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }

}